package com.naihe.anderson.sms.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.ClipboardManager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.adapter.SearchMsgAdapter;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.entity.Nhsms;
import com.naihe.anderson.sms.listener.OnNotifyDataNhSmsListener;
import com.naihe.anderson.sms.manager.NhSmsMgr;
import com.naihe.anderson.sms.widgets.LongClickDialog;

public class SearchActivity extends Activity implements OnItemClickListener,OnClickListener,OnNotifyDataNhSmsListener,OnItemLongClickListener {
	
	Button backBtn;
	ImageButton searchMnsBtn;
	EditText searchMnsEdit;
	NhSmsMgr nhSmsMgr;
	
	ProgressBar reloading_progress;
	
	ListView searchdateList;
	SearchMsgAdapter searchMsgAdapter;
	List<Nhsms> searchLists = new ArrayList<Nhsms>();
	
	private static final int ITEM_LONG_CLICK_COPY_INDEX = 0;
	
	public static final int MSG_WHAT_DATA_CHANGE = 0;
	public static final int MSG_WHAT_GET_DATA_CHANGE = 1;
	public final static int MSG_WHAT_SHOW_PROGRESS_WHAT = 2;
	
	Handler mHandler = new Handler(){

		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_WHAT_DATA_CHANGE:
				dataChange((List<Nhsms>) msg.obj);
				break;
			case MSG_WHAT_GET_DATA_CHANGE://数据变化刷新
				if(reloading_progress.isShown())
					reloading_progress.setVisibility(View.GONE);
				break;
			case MSG_WHAT_SHOW_PROGRESS_WHAT:
				reloading_progress.setVisibility(View.VISIBLE);
				break;
			default:
				break;
			}
		}
		
	};
	
	private void dataChange(List<Nhsms> dataChangeList){
		searchLists.clear();
		searchLists.addAll(dataChangeList);
		
		searchMsgAdapter.notifyDataSetChanged();
		mHandler.sendEmptyMessage(MSG_WHAT_GET_DATA_CHANGE);
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_message_layout);
        nhSmsMgr = ((NhSmsApp)getApplication()).getNhSmsMgr();
        nhSmsMgr.setDataSetChangForSmsListener(this, -1);
        initView();
    }

	private void initView(){
		backBtn = (Button) findViewById(R.id.back_btn);
		backBtn.setOnClickListener(this);
		
		reloading_progress = (ProgressBar) findViewById(R.id.reloading_progress);
		
		searchMnsBtn = (ImageButton) findViewById(R.id.search_mns_btn);
		searchMnsBtn.setOnClickListener(this);
		
		searchMnsEdit = (EditText) findViewById(R.id.search_mns_edit);
		searchdateList = (ListView) findViewById(R.id.searchdate_list);
		
		searchMsgAdapter = new SearchMsgAdapter(this, searchLists);
		searchdateList.setAdapter(searchMsgAdapter);
		searchdateList.setOnItemLongClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.back_btn){
			finishActivityThing();
		} else if(v.getId() == R.id.search_mns_btn){
			String key = searchMnsEdit.getText().toString();
			if(!TextUtils.isEmpty(key) && !searchMsgAdapter.getOldSearchKey().equals(key)){
				searchMsgAdapter.setSearchKey(key);
				mHandler.sendEmptyMessage(MSG_WHAT_SHOW_PROGRESS_WHAT);
				nhSmsMgr.searchNhsmsByBody(key);
			}
			hiddenImm(v);
		}
	}
	
	/**
     * 隐藏输入法
     *
     * @param view
     */
    private void hiddenImm(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
	
	private void finishActivityThing(){
		nhSmsMgr.setDataSetChangForSmsListener(null, -1);
		finish();
		searchLists.clear();
		searchLists = null;
		System.gc();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK ){
        	finishActivityThing();
			return true;
		}
		
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		
	}

	@Override
	public void onDataChanged(List<Nhsms> dataChangeList) {
		Message message = mHandler.obtainMessage();
		message.what = MSG_WHAT_DATA_CHANGE;
		message.obj = dataChangeList;
		mHandler.sendMessage(message);
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		Nhsms nhsms = searchMsgAdapter.getItem(position);
		final String content = nhsms.getBody();
		String[] links = new String[]{getResources().getString(R.string.msg_onlong_click_copy)};
		
		new LongClickDialog(SearchActivity.this, links, getResources().getString(R.string.operation), new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(position == ITEM_LONG_CLICK_COPY_INDEX){
					ClipboardManager clipBoard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
					clipBoard.setText(content);
				}
			}
		}).show();
		return false;
	}
}